plants = {}

function plants.register_crop(name, desc, bit1, bit2)
   bit1 = bit1 or 0
   bit2 = bit2 or 0
   minetest.register_node('plants:'..name..'_1', {
      description = desc,
      tiles = {'plants_'..name..'_1.png'},
      drawtype = 'plantlike',
      paramtype = 'light',
      paramtype2 = 'meshoptions',
      walkable = false,
      drop = 'plants:'..name..'_2',
      groups = {breakable=1, not_in_creative_inventory=1},
      selection_box  = {
         type = 'fixed',
         fixed = {{-.4, -.5, -.4, .4, -.2, .4}}
      },
      on_timer = function(pos)
         local node = minetest.get_node(pos)
         minetest.set_node(pos, {name = 'plants:'..name..'_2', param2 = node.param2})
      end,
   })

   minetest.register_node('plants:'..name..'_2', {
      description = desc,
      tiles = {'plants_'..name..'_2.png'},
      inventory_image = 'plants_'..name..'_2.png',
      drawtype = 'plantlike',
      paramtype = 'light',
      paramtype2 = 'meshoptions',
      walkable = false,
      groups = {breakable=1},
      selection_box  = {
         type = 'fixed',
         fixed = {{-.4, -.5, -.4, .4, .25, .4}}
      },
      after_place_node = function(pos)
         local bitflag = bit1 + bit2
         minetest.set_node(pos, {name = 'plants:'..name..'_2', param2 = bitflag})
      end,
      on_punch = function(pos, node, puncher, pointed_thing)
         local player_inv = puncher:get_inventory()
         if not player_inv:contains_item('main', {name='plants:'..name, count = 4}) then
            local timer = minetest.get_node_timer(pos)
            player_inv:add_item('main', 'plants:'..name)
            minetest.set_node(pos, {name = 'plants:'..name..'_1', param2 = node.param2})
            timer:start(300)
         else
            minetest.chat_send_player(puncher:get_player_name(), 'You have too many of these already.')
         end
      end,
   })

   minetest.register_craftitem('plants:'..name, {
      description = desc,
      inventory_image = 'plants_'..name..'.png',
      stack_max = 1,
      groups = {not_in_creative_inventory=1},
      on_drop = lobby.no_drop
   })

end

function plants.register_grass(name, desc)
   minetest.register_node('plants:'..name..'_1', {
      description = desc,
      tiles = {'plants_'..name..'_1.png'},
      inventory_image = 'plants_'..name..'_3.png',
      drawtype = 'plantlike',
      paramtype = 'light',
      paramtype2 = 'degrotate',
      walkable = false,
      groups = {breakable=1},
      selection_box  = {
         type = 'fixed',
         fixed = {{-.3, -.5, -.3, .3, 0, .3}}
      },
      after_place_node = function(pos)
         local rot = math.random(0,179)
         local i = math.random(1,5)
         minetest.set_node(pos, {name = 'plants:'..name..'_'..i, param2 = rot})
      end,
      on_timer = function(pos)
         local node = minetest.get_node(pos)
         local meta = minetest.get_meta(pos)
         local name = meta:get_string('flower')
         if name ~= '' then
            minetest.set_node(pos, {name = 'plants:'..name, param2 = node.param2})
         end
      end,
   })
   for i = 2, 5 do
      minetest.register_node('plants:'..name..'_'..i, {
         description = desc,
         tiles = {'plants_'..name..'_'..i..'.png'},
         drawtype = 'plantlike',
         paramtype = 'light',
         paramtype2 = 'degrotate',
         walkable = false,
         groups = {breakable=1, not_in_creative_inventory=1},
         drop = 'plants:'..name..'_1',
         selection_box  = {
            type = 'fixed',
            fixed = {{-.3, -.5, -.3, .3, 0, .3}}
         },
      })
   end
end

function plants.register_plant(name, desc, light)
   minetest.register_node('plants:'..name, {
      description = desc,
      tiles = {'plants_'..name..'.png'},
      inventory_image = 'plants_'..name..'.png',
      light_source = light,
      drawtype = 'plantlike',
      paramtype = 'light',
      paramtype2 = 'degrotate',
      walkable = false,
      groups = {breakable=1},
      selection_box  = {
         type = 'fixed',
         fixed = {{-.3, -.5, -.3, .3, 0, .3}}
      },
      after_place_node = function(pos)
         local rot = math.random(0,179)
         minetest.set_node(pos, {name = 'plants:'..name, param2 = rot})
      end,
   })
end

function plants.register_flower(name, desc, light)
   minetest.register_node('plants:'..name, {
      description = desc,
      tiles = {'plants_'..name..'.png'},
      inventory_image = 'plants_'..name..'.png',
      light_source = light,
      drawtype = 'plantlike',
      paramtype = 'light',
      paramtype2 = 'degrotate',
      walkable = false,
      groups = {breakable=1},
      selection_box  = {
         type = 'fixed',
         fixed = {{-.3, -.5, -.3, .3, 0, .3}}
      },
      after_place_node = function(pos)
         local rot = math.random(0,179)
         minetest.set_node(pos, {name = 'plants:'..name, param2 = rot})
      end,
      on_punch = function(pos, node, puncher, pointed_thing)
         local player = puncher:get_player_name()
         local wield = puncher:get_wielded_item()
         local wield_name = wield:get_name()
         if wield_name == 'creative:tool_breaking' then
            return
         else
            local player_inv = puncher:get_inventory()
            if not player_inv:contains_item('main', {name='plants:'..name, count = 4}) then
               local timer = minetest.get_node_timer(pos)
               local meta = minetest.get_meta(pos)
               player_inv:add_item('main', 'plants:'..name)
               minetest.set_node(pos, {name = 'plants:grass_1', param2 = node.param2})
               meta:set_string('flower', name)
               timer:start(300)
            else
               minetest.chat_send_player(puncher:get_player_name(), 'You have too many of these already.')
            end
         end
      end,
      on_drop = lobby.no_drop,
   })
end

dofile(minetest.get_modpath('plants')..'/bushes.lua')
dofile(minetest.get_modpath('plants')..'/crops.lua')
dofile(minetest.get_modpath('plants')..'/flowers.lua')
dofile(minetest.get_modpath('plants')..'/fungi.lua')
dofile(minetest.get_modpath('plants')..'/grass.lua')
dofile(minetest.get_modpath('plants')..'/misc.lua')
dofile(minetest.get_modpath('plants')..'/sapling.lua')
